package svgedit.xml;

import java.awt.RenderingHints;
import java.awt.Shape;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

import svgedit.gui.Circle;
import svgedit.gui.Frame;
import svgedit.gui.Line;
import svgedit.gui.Rect;
import svgedit.svg.SVGDocument;
 /**
  * Will eventually read from a List(ArrayList/LinkedList) and write to a XML file
  * accordingly. 
  * @author hirundak
  *
  */
public class XMLWriter 
{
  	private SVGDocument document;
	public void write(File fileName, Frame frame) throws Exception 
	{    
	    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
	    
	    Document document = documentBuilder.newDocument();
	   //DocumentType doc = document.create
	   // Comment simpleComment = document.createComment("DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd");
	    Element rootElement = document.createElement("svg");
	    rootElement.setAttribute("width", "100%");
	    rootElement.setAttribute("height", "100%");
	    rootElement.setAttribute("xmlns", "http://www.w3.org/2000/svg");
	    rootElement.setAttribute("version", "1.1");
	    document.appendChild(rootElement);
	    
	    for (Rect rect : frame.view.rects) 
	    {
				Element rectElement = document.createElement("rect");
	    		rectElement.setAttribute("x", Integer.toString(rect.x1));
	    		rectElement.setAttribute("y", Integer.toString(rect.y1));
	    		rectElement.setAttribute("stroke-width", Integer.toString(rect.getStrokeWidth()));
	    		if(rect.getFillColor() != null){
	    			rectElement.setAttribute("fill", "#"+Integer.toHexString( rect.getFillColor().getRGB() & 0x00ffffff ));

	    		}else{
	    			rectElement.setAttribute("fill", "none");
	    		}
	    		if(rect.getStrokeColor() != null){
	    			//rectElement.setAttribute("stroke", "#" + Integer.toHexString( rect.getStrokeColor().getRGB() & 0x00ffffff ));   			
	    		}
	    		else{
	    			rectElement.setAttribute("stroke", "black");
	    		}
	    		rectElement.setAttribute("width",  Integer.toString(rect.getWidth()));
	    		rectElement.setAttribute("height", Integer.toString(rect.getHeight()));
	    		rootElement.appendChild(rectElement);
		}
		for (Line line : frame.view.lines) 
		{
				Element lineElement = document.createElement("line");
				lineElement.setAttribute("x1", Integer.toString(line.getX1()));
	    		lineElement.setAttribute("y1", Integer.toString(line.getY1()));
	    		lineElement.setAttribute("x2", Integer.toString(line.getX2()));
	    		lineElement.setAttribute("y2", Integer.toString(line.getY2()));
	    		lineElement.setAttribute("stroke-width", Integer.toString(line.getStrokeWidth()));
	    		if(line.getFillColor() != null){
	    			lineElement.setAttribute("fill", "#" + Integer.toHexString( line.getFillColor().getRGB() & 0x00ffffff ));
	    		}else{
	    			lineElement.setAttribute("fill", "none");
	    		}
	    		if(line.getStrokeColor() != null){
	    			//lineElement.setAttribute("stroke", "#" + Integer.toHexString( line.getStrokeColor().getRGB() & 0x00ffffff ));

	    		}else{
	    			lineElement.setAttribute("stroke", "black");
	    		}
	    		rootElement.appendChild(lineElement);
		}
		for (Circle circle : frame.view.circles) 
		{
				Element circleElement = document.createElement("circle");
				//Really strange thing happens here! When it getRadius(), apparently it gets twice number of radius
				//and getX() and getY() doesn't return the correct value.
				circleElement.setAttribute("cx", Integer.toString(circle.getX()+45));
	    		circleElement.setAttribute("cy", Integer.toString(circle.getY()+45));
	    		circleElement.setAttribute("r", Integer.toString(circle.getRadius()/2));
	    		circleElement.setAttribute("stroke-width", Integer.toString(circle.getStrokeWidth()));
	    		if(circle.getFillColor() != null){
	    			circleElement.setAttribute("fill", "#" + Integer.toHexString( circle.getFillColor().getRGB() & 0x00ffffff ));
	    		}else{
	    			circleElement.setAttribute("fill", "none");
	    		}
	    		if(circle.getStrokeColor() != null){
	    			//circleElement.setAttribute("stroke", "#" + Integer.toHexString( circle.getStrokeColor().getRGB() & 0x00ffffff ));
	    		}else{
	    			circleElement.setAttribute("stroke", "black");
	    		}
	    		rootElement.appendChild(circleElement);
		}

	    TransformerFactory transformerFactory = TransformerFactory.newInstance();
	    Transformer transformer = transformerFactory.newTransformer();
	    transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
	    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd");
	    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	    DOMSource source = new DOMSource(document);
	    StreamResult result =  new StreamResult(fileName);
	    transformer.transform(source, result);
	    }

  }

